# from django.db import models
# from user.models import UserInfo  # 引用 UserInfo 模型

# class ChatRoom(models.Model):
#     name = models.CharField(max_length=100, unique=True)

#     def __str__(self):
#         return self.name

# class Message(models.Model):
#     user = models.ForeignKey(UserInfo, on_delete=models.CASCADE)  # 使用 UserInfo 模型
#     room = models.ForeignKey(ChatRoom, on_delete=models.CASCADE)
#     content = models.TextField()
#     timestamp = models.DateTimeField(auto_now_add=True)

#     def __str__(self):
#         return f'{self.user.username} - {self.content}'

from django.db import models
from user.models import UserInfo  # 引用 UserInfo 模型
from goods.models import GoodsInfo  # 引用 GoodsInfo 模型

class ChatRoom(models.Model):
    name = models.CharField(max_length=100, unique=True)
    good = models.ForeignKey(GoodsInfo, on_delete=models.CASCADE,default = -1)
    participants = models.ManyToManyField(UserInfo)

    def __str__(self):
        return self.name


class Message(models.Model):
    user = models.ForeignKey(UserInfo, on_delete=models.CASCADE)  # 使用 UserInfo 模型
    room = models.ForeignKey(ChatRoom, on_delete=models.CASCADE)
    content = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f'{self.user.user.username} - {self.content}'  # 使用 self.user.user.username 获取用户名